import 'package:flutter/material.dart';
import 'package:flutterclue/mvp/mode/ClueCountBean.dart';
import 'package:flutterclue/utils/ShowSnackBarUtil.dart';
import 'package:flutterclue/utils/ToastUtilForAndroid.dart';
import 'ClueListPage.dart';
import 'package:flutterclue/mvp/presenter/ClueManagerPagePresenter.dart';

///线索详情
class ClueManagerPage extends StatefulWidget {
  final int timeType;

  ClueManagerPage({this.timeType});

  @override
  _ClueManagerPageState createState() => new _ClueManagerPageState();
}

class _ClueManagerPageState extends State<ClueManagerPage>
    with SingleTickerProviderStateMixin
    implements ClueManagerView {
  ClueManagerPagePresenter _managerPagePresenter =
      new ClueManagerPagePresenter();

  TabController _tabController;
  List<TabViewBean> _tabBeanList = new List();
  List<Tab> _tabList = new List();

  @override
  void didUpdateWidget(ClueManagerPage oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
    _managerPagePresenter.getClueCount(widget.timeType);
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabBeanList
        .add(new TabViewBean(tab: new Tab(text: "未处理"), entryState: -1));
    _tabBeanList.add(new TabViewBean(tab: new Tab(text: "跟进中"), entryState: 1));
    _tabBeanList
        .add(new TabViewBean(tab: new Tab(text: "车辆到店"), entryState: 3));
    _tabBeanList.add(new TabViewBean(tab: new Tab(text: "流失"), entryState: 4));
    _tabController =
        new TabController(length: _tabBeanList.length, vsync: this);
    _managerPagePresenter.attachView(this);
    _managerPagePresenter.getClueCount(widget.timeType);
  }

  @override
  void dispose() {
    super.dispose();
    _tabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Container(
      child: new Scaffold(
        appBar: new TabBar(
            tabs: _tabBeanList.map((bean) {
              return bean.tab;
            }).toList(),
            indicatorWeight: 1,
            indicatorSize: TabBarIndicatorSize.label,
            controller: _tabController,
            labelColor: Theme.of(context).accentColor,
            unselectedLabelColor: Colors.black,
            indicatorColor: Theme.of(context).accentColor),
        body: new TabBarView(
            controller: _tabController,
            children: _tabBeanList.map((TabViewBean tab) {
              return new ClueListPage(tab.entryState, widget.timeType);
            }).toList()),
      ),
    );
  }

  @override
  dismissLoadingView() {}

  @override
  void onFail(String s) {
    ToastUtil.showShort(s);
  }

  @override
  void onSuccessData(ClueCountBean clueCountBean) {
    _tabList.clear();
    _tabBeanList.clear();
    _tabBeanList.add(new TabViewBean(
        tab: new Tab(
            child: Text("未处理 ${clueCountBean.UntreatedTotalCount}",
                style: TextStyle(fontSize: 13,),softWrap: false,)),
        entryState: -1));
    _tabBeanList.add(new TabViewBean(
        tab: new Tab(
            child: Text("跟进中 ${clueCountBean.FollowUpTotalCount}",
                style: TextStyle(fontSize: 13),softWrap: false,)),
        entryState: 1));
    _tabBeanList.add(new TabViewBean(
        tab: new Tab(
            child: Text("车辆到店 ${clueCountBean.VehicleToStoreTotalCount}",
                style: TextStyle(fontSize: 13),softWrap: false,)),
        entryState: 3));
    _tabBeanList.add(new TabViewBean(
        tab: new Tab(
            child: Text("流失 ${clueCountBean.LossTotalCount}",
                style: TextStyle(fontSize: 13),softWrap: false,)),
        entryState: 4));
    setState(() {});
  }

  @override
  showLoadingView() {}
}

class TabViewBean {
  Tab tab;
  int entryState;

  TabViewBean({this.tab, this.entryState});
}
